<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php include("mainlist.php");?>
<table cellspacing="0" border="0" width="995">
	<tr>
		<td width="200" valign="top">					
			<?php
				include("menuleftcategory.php");
			?>
		</td>	
		<td width="795">
			<form name="myForm" action="index.php?admin=productAdmin&do=formaddproduct" method="post" enctype="multipart/form-data">
				<table cellspacing="0" border="0" width="790">
					<tr>
						
						<td width="790" class="title_manager_cate">Thêm mới File</td>
					
					</tr>
				</table>
				<table class="reference" cellspacing="0" border="1" width="100%"> 
					<tr>
						<td colspan="2" align="center">
							<?php
							
								$cateID=$_POST['cateID'];
								$groupID = $_POST['groupCat'];
								$productname=$_POST['productname'];
								
								
								$target="../upload/products/";
								
								$link = $target.$_FILES['productImage']['name'];
								move_uploaded_file($_FILES['productImage']['tmp_name'],$link);
								
								$productDetail = $_POST['productDetail'];
								
								/*$productPrice = $_POST['productPrice'];
								$promotionPrice = $_POST['promotionPrice'];
								$prostyle = $_POST['prostyle'];
								$productWarranty = $_POST['productWarranty'];
								
								$dvt = $_POST['dvt'];
								if(!empty($_POST['productHot'])){
										$productHot = $_POST['productHot'];	
									}else{
										$productHot = 0;	
									}
									
								if(!empty($_POST['promotion'])){
									$promotion = $_POST['promotion'];
								}else{
									$promotion=0;
								}
								
								if(!empty($_POST['prostyle'])){
									$prostyle = $_POST['prostyle'];
								}else{
									$prostyle=0;
								}*/	
									
									$state = $_POST['state'];
									
									$date = date("d-m-Y");
									$ts = strtotime($date);
									
									
								
								if(isset($_POST['submit'])){
									
									if($productname==''){
										echo "<span class=\"error_message\">Bạn phải nhập vào tên File.</span>";
									}
									
									
									if($productname!=''){
										
										$query = "INSERT INTO productdetails(groupID,productname,imgpath,productDetail,productView,productQty,position,dateCreated,state) VALUES ('$groupID','$productname','$link','$productDetail','','','','$ts','$state')";
										if (mysql_query($query))
										{
											if ($cateID){
												$newProductId = 0;
												 foreach ($cateID as $aId)
												 {
												 	
												 	if (!$newProductId) {
												 		$newProductId = mysql_insert_id();
												 	}
												 	
												 	$query = "INSERT INTO categories_products(categoryId,productId) VALUES ('$aId','$newProductId')";
												 	if (mysql_query($query))
												 	{
												 		echo "Thêm file thành công.";
												 	}else {
												 		echo "Lỗi khi thêm file. Vui lòng thử lại.";
												 	}
												 }
											}
											
											
										}else {
											echo "Lỗi khi thêm file. Vui lòng thử lại.";
										}
										echo "<script>window.location='index.php?admin=productAdmin&do=listproduct'</script>";
									}	
								}		
							?>	
						</td>
					</tr>	
					<tr>
						<td width="200">Tên File</td>
						<td width="400">
							<input type="text" name="productname" size="50" /><font color="#FF0000"> * </font>
						</td>
					</tr>
					
					<tr>
						<td>Danh mục cha:</td>
						<td>
							
							<!-- <select multiple name="cateID" id="cateID" onchange="ajaxFunction()">-->
							<select name="cateID[]" id="cateID" multiple> 
								<?php					
								$query = "select * from categories";
								$result = mysql_query($query);
								while($row = mysql_fetch_array($result)){?>
									<option value="<?php echo $row['id']?>"><?php echo $row['cateName'];?></option>	
								<?php
								}
								?>
							 </select>	   
						</td>
					</tr>
					
					<tr>
						<td>Đường dẫn File:</td>
						<td>
							<input type="file" name="productImage" />
						</td>
					</tr>
					<tr>
						<td>Mô tả File:</td>
						<td>
						   <textarea name="productDetail" rows="15"></textarea>
						</td>
					</tr>
					
					<tr>
						<td>Trạng thái:</td>
						<td>
							<select name="state">
								<option value="on">Hiện tin</option>
								<option value="off">Ẩn tin</option>
							</select>
						</td>
					</tr>
					
					
				   <tr>
						<td></td>
						<td>
							<input type="submit" name="submit" value="Tạo File mới" />
							<input type="reset" name="reset" value="Nhập lại" />
							<input type="button" name="Cancel" value="Cancel" onclick="window.location = 'index.php?admin=productAdmin&do=listproduct' " /> 
						<td>
				   </tr>
				</table>
			</form>
		</td>
	</tr>	
</table>
</body>
</html>
